package com.nineclock.system.controller;

import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.SysCompanyContactConfigController.OrganizationControllerApi;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentDTOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    private SysCompanyUserService sysCompanyUserService;

    @Autowired
    private SysDepartmentDTOService departmentDTOService;

    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> listSysCompanyUser() {
        List<SysCompanyUserDTO> userDTOList = sysCompanyUserService.listSysCompanyUser();
        return Result.success(userDTOList);
    }

    //获取组织架构列表(左侧列表
    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartmentList() {
        List<SysDepartmentDTO> sysDepartmentDTOList = departmentDTOService.queryDepartmentList();
        return Result.success(sysDepartmentDTOList);
    }

    /*分页查询部门下的员工数据列表*/
    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> members(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                                         String keyword,
                                                        @RequestParam(value = "departmentId",required = false,defaultValue = "o") Long departmentId) {

        PageResult<SysCompanyUserDTO> pageRest=sysCompanyUserService.members(page,pageSize,keyword,departmentId);
        return Result.success(pageRest);
    }

    /**
     *直接导入员工
     *
     */

    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
            sysCompanyUserService.uploadExcel(excelFile);

        return Result.success();
    }
/**
 * 查看顶级部门的id
 * */
    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> departmentIds = departmentDTOService.queryDepartmentIds();
        return Result.success(departmentIds);
    }

    /**
     *
     * 获取部门下的所有员工
     *
     * @return*/

    @Override
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentIds(
            @RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count= sysCompanyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }

    /**
     * 获取简单的列表
     */


    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(
            @RequestParam(value = "id",required = false,defaultValue = "0" )Long departmentId,
            Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> simpleDepartmentUser  =departmentDTOService.querySimpleDepartment(departmentId,includeMember);

        return Result.success(simpleDepartmentUser);
    }


}
